package com.example.demo_job.service.impl;


import com.example.demo_job.entity.Company;
import com.example.demo_job.entity.Position;
import com.example.demo_job.entity.Seeker;
import com.example.demo_job.mapper.CompanyMapper;
import com.example.demo_job.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class CompanyServiceimpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Company findByUsername(String cusername) {
        return companyMapper.findByUsername(cusername);
    }

    @Override
    public Company login(String cusername, String cpassword) {
        return companyMapper.login(cusername, cpassword);
    }

    @Override
    public int register(String cusername, String cpassword, String cname, String cyear, String caddress, String cemail, String cphone) {
        String cinfo=cusername+".txt";
//        File txtfile=new File("C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\CompanyTxt\\"+cinfo);
        File txtfile=new File("src\\main\\resources\\static\\CompanyTxt\\"+cinfo);
        try {
            txtfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return companyMapper.register(cusername,cpassword,cname,cyear,caddress,cemail,cphone,cinfo);
    }

    @Override
    public String showCompanyProfile(String cinfo) {
        return readTxtByinfo("CompanyTxt/",cinfo);
    }

    @Override
    public String showPositionDescription(String pinfo) {
        return readTxtByinfo("PositionTxt/",pinfo);
    }

    @Override
    public String readTxtByinfo(String choiceInfo,String info) {
        //最终路径
//        String absolutePath="C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\"+choiceInfo+info;
//        String absolutePath="src\\main\\resources\\static\\"+choiceInfo+info;
        Resource resource=new ClassPathResource("static/"+choiceInfo+info);
        BufferedReader reader = null;
        String total_line = null;
        try {
            reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (total_line != null) {
                    total_line += "\n" + line; // 在每行前面添加一个换行符
                } else {
                    total_line = line; // 如果是第一行，直接赋值
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); // 在调试阶段可以使用，生产环境中应更妥善处理异常
        } finally {
            if (reader != null) {
                try {
                    reader.close(); // 关闭BufferedReader
                } catch (IOException e) {
                    e.printStackTrace(); // 生产环境中应更妥善处理异常
                }
            }
        }
        return total_line;
    }

    @Override
    public String FindCinfo(String cusername) {
        String info=companyMapper.findInfo(cusername);
        if(info==null) return null;
//        String Tinfo="C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\CompanyTxt\\"+info;
        String Tinfo="src\\main\\resources\\static\\CompanyTxt\\"+info;
        return Tinfo;
    }

    @Override
    public void Write(String tsinfo, String cv) {
        //tsinfo绝对路径，cv文本内容
        // 创建文件输出流
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(tsinfo));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将文本数据写入文件
        try {
            writer.write(cv);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭文件输出流
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateComMessage(String cusername, String cname, String cyear, String caddress, String cemail, String cphone) {
        companyMapper.UpdateCompany(cusername,cname,cyear,caddress,cemail,cphone);
    }

    @Override
    public List<Seeker> showTenSeeker() {
        List<Seeker> seekerList = companyMapper.showseekers();
        //排序
        Collections.sort(seekerList, new Comparator<Seeker>() {
            @Override
            public int compare(Seeker o1, Seeker o2) {
                return o2.getSedu() - o1.getSedu();
            }
        });
        //输出前十名
        if (seekerList.size() >= 10) {
            List<Seeker> topTenseekerList = seekerList.subList(0, 10);
            return topTenseekerList;
        }
        return seekerList;
    }

}
